<template>
  <div class="screen" v-if="is_show_screen" :class="is_top?'box_top':''">
    <!-- 筛选条件 -->
    <div class="screen_content">
      <div v-if="is_show_state">
        <div class="state"><span>●</span>状态</div>
        <div class="state_title">
          <div 
            :class="-1 == state_num ? 'current_state_title':''"
            @click="assignment('state_num',-1)"
          >
            全部
          </div>
          <div 
            v-for="(val,index) in state_list" :key="index" 
            :class="index == state_num ? 'current_state_title':''"
            @click="assignment('state_num',index)"
          >
            {{val.name}}
          </div>
        </div>
      </div>
      <div>
        <div class="type"><span>●</span>类型</div>
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="getWorkFlows"
          >
            <div class="type_title">
              <div :class="-1 == type_num  ? 'current_type_title':''" @click="assignment('type_num',-1)">全部</div>
              <div 
                v-for="(val,index) in type_list" :key="index" 
                :class="index == type_num  ? 'current_type_title':''"
                @click="assignment('type_num',index)"
              >
                {{val.name}}
              </div>
            </div>
          </van-list>
      </div>
    </div>
    
    <div class="btn">
      <van-button type="primary"  block color="#5DD3F9" @click="hide">确认</van-button>
    </div>
  </div>
</template>

<script>
export default {
  props:{
    is_show_state:{
      type:Boolean,
      required:true,
    },
    is_top:{
      type:Boolean,
      required:true,
    },
  },
  data(){
    return{
      is_show_screen: false,
      state_num:-1,
      type_num:-1,
      type_list:[],
      state_list:[
        {
          id:'0',
          name:'草稿中'
        },
        {
          id:'1',
          name:'进行中'
        },
        {
          id:'2',
          name:'被退回'
        },
        {
          id:'3',
          name:'被撤回'
        },
        {
          id:'4',
          name:'已完成'
        },
        {
          id:'5',
          name:'已关闭',
        }
      ],
      current_state_num:-1,
      current_type_num:-1,
      finished:false,
      loading:false,
      page:1,
    }
  },
  created(){
    this.getWorkFlows()
  },
  methods:{
    getWorkFlows(){
      this.loading = true
      let data = {
        page:this.page,
      }
      this.$store.dispatch('api_workflows',data).then(res => {
        res.data.data.value.forEach(item=>{
          this.type_list.push(item)
        })
        this.loading = false
        this.page++
        
        // 数据全部加载完成
        if(this.type_list.length >=  res.data.data.total){
          this.finished = true;
        }
      }).catch(err=>{
        this.loading = false
        this.finished = true;
      })
    },
    assignment(name,index){
      this[name] = index
    },
    clear(){
      this.current_state_num = -1
      this.current_type_num = -1
    },
    // 显示
    show(){
      this.state_num = this.current_state_num
      this.type_num = this.current_type_num 
      this.is_show_screen = true
      window.history.pushState(null,null,'#')
    },
    // 关闭
    hide(){
      this.is_show_screen = false
      this.current_state_num = this.state_num
      this.current_type_num = this.type_num
      let type_obj = this.type_num == -1 ?{}:this.type_list[this.type_num]
      let state_obj = this.state_num == -1 ?{}:this.state_list[this.state_num]
      this.$parent.confirm(type_obj,state_obj)
      this.$router.go(-1)
    }
  },
  mounted(){
    // dom渲染成功后 监听popstate 事件
    window.addEventListener('popstate',e => {
      if(this.is_show_screen){
        this.is_show_screen = !this.is_show_screen
      }
    },false)
  },
  beforeDestroy(){
    // 页面销毁前 移除popstate 事件
    window.removeEventListener('popstate',e => {
      if(this.is_show_screen){
        this.is_show_screen = !this.is_show_screen
      }
    },false)
  },
}
</script>

<style lang="less" scoped>
.box_top{
  top:0px !important;
}
.screen{
  position: absolute;
  top:-45px;
  bottom:0px;
  width: 100%;
  .screen_content{
    background: #fff;
    width:100%;
    height: auto;
    position: absolute;
    top:0px;
    bottom:50px;
    overflow-y: auto;
    padding-bottom: 10px;
    >:nth-child(2){
      position: relative;
      margin-bottom: 25px;
      .more{
        position: absolute;
        bottom: -25px;
        right: 20px;
      }
    }
    .state{
      margin-top: 10px;
      margin-left: 10px;
      font-size: 14px;
      span{
        color:#4DA9EC;
        font-size: 18px;
        margin-right: 5px;
      }
    }
    .state_title{
      font-size: 12px;
      padding:0 20px 0 20px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      >div{
        width: 28%;
        height: 40px;
        line-height: 40px;
        border-radius: 10px;
        margin-top: 10px;
        background: #F4F4F4;
        text-align: center;
      }
    }
    .type{
      margin-left: 10px;
      margin-top: 10px;
      font-size: 14px;
      span{
        color:#17C295;
        font-size: 18px;
        margin-right: 5px;
      }
    }
    .type_title{
      font-size: 12px;
      padding:0 20px 0 20px;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      >div{
        width: 47%;
        height: 40px;
        line-height: 40px;
        border-radius: 10px;
        margin-top: 10px;
        background: #F4F4F4;
        text-align: center;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
    .current_state_title{
      background: #E5F2FB !important;
      color:#4DA9EC;
    }
    .current_type_title{
      background: #DCF5EF !important;
      color:#17C295;
    }
  }
  .btn{
    position: absolute;
    bottom: 0px;
    left:0;
    right: 0;
    display: flex;
    height: 50px;
    padding:10px 10px 0 10px;
    background: #fff;
    justify-content: space-around;
    .van-button{
      // width:42%;
      height: 40px;
      // border:none;
      border-radius: 5px;
      line-height: 40px;
    }
  }
}
</style>